import numpy as np
np.random.seed(42)
y_true = np.random.randint(0,2,size=5)
print(y_true)

y_pred= np.ones(5,dtype=np.int32)
print(y_pred)

print(np.sum(y_true == y_pred)/len(y_true))

from sklearn import metrics
print(metrics.accuracy_score(y_true, y_pred))


truly_a_positive = (y_true == 1)
predicted_a_positive = (y_pred == 1)
true_positive = np.sum(predicted_a_positive*truly_a_positive)

print(true_positive)
print(truly_a_positive)    #[False  True False False False]
print(predicted_a_positive)  #[ True  True  True  True  True]

false_positive = np.sum((predicted_a_positive == 1)* (truly_a_positive==0))
print(false_positive)

false_negative = np.sum((predicted_a_positive == 0)* (truly_a_positive==1))
print(false_negative)

true_negative = np.sum((predicted_a_positive == 0)* (truly_a_positive==0))
print(true_negative)

accuracy = (true_positive+true_negative)/len(y_true)  #准确率
print(accuracy)

#精确率
precision = true_positive/(true_positive+false_positive)  #精确率
print(precision)

print(metrics.precision_score(y_true,y_pred))

#召回率
recall = true_positive/(true_positive+false_negative)
print(recall)
print(metrics.recall_score(y_true, y_pred))

x=np.linspace(0,10,100)
y_true = np.sin(x) +np.random.rand(x.size)-0.5
y_pred = np.sin(x)
import matplotlib.pyplot as plt
plt.plot(x,y_true,'o', label='data')
plt.plot(x,y_pred,linewidth = 1, label = 'model')
plt.xlabel('x')
plt.ylabel('y')
plt.legend(loc='lower left')
plt.show()

mse = np.mean((y_true-y_pred)**2)
print('mse:',mse)  #mse: 0.08531839480842378

print(metrics.mean_squared_error(y_true, y_pred))

fvu = np.var(y_true-y_pred)/np.var(y_true)
print('fvu:',fvu)  #fvu: 0.163970326266295
fve = 1-fvu
print(fve)         #0.836029673733705

print(metrics.explained_variance_score(y_true, y_pred))   #0.836029673733705

r2 = 1.0-mse/np.var(y_true)
print(r2)              #0.8358169419264746

print(metrics.r2_score(y_true, np.mean(y_true)*np.ones_like(y_true)))  #0